package com.mtz.controller;

import com.mtz.VO.ResultVO;
import com.mtz.dateobject.Question;
import com.mtz.service.QuestionService;
import com.mtz.utils.ResultVOUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 煜赜
 * @date 2018/11/27 0027 上午 10:33
 */
@Api(description = "问题接口")
@RestController
@RequestMapping("/ques")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    /**
     * 通过问题id查找问题
    * @param id 问题id
     * @return 对应id的问答记录
     */
    @ApiOperation(value = "通过问题id查找问题", notes = "通过问题id查找问题")
    @GetMapping("/id")
    public ResultVO findById(@ApiParam(value = "问题id",required = true)
                                 @RequestParam("id") Integer id) {
        Question question = questionService.findQuesById(id);

        return ResultVOUtil.success(question);
    }

    /**
     * 通过关键词查找问题集
     * @param keyword 问题描述关键词
     * @return 对应关键词的问答集合
     */
    @ApiOperation(value = "通过关键词查找问题集", notes = "通过关键词查找问题集")
    @GetMapping("/keyword")
    public ResultVO findByKeyword(@ApiParam(value = "keyword",required = true)
                                      @RequestParam("keyword") String keyword) {
        List<Question> questionList = questionService.findQuesByKeyword(keyword);
        return ResultVOUtil.success(questionList);
    }

    /**
     * 通过知识类型查找问题集
     * @param knowledgeType 知识类型
     * @return
     */
    @ApiOperation(value = "通过类目查找问题集", notes = "通过类目查找问题集")
    @GetMapping("/knowledgeType")
    public ResultVO findByKnowledgeType(@ApiParam(value = "知识类目",required = true)
                                            @RequestParam("knowledgeType") Integer knowledgeType) {
        List<Question> questionList = questionService.findQuesByKnowledgeType(knowledgeType);
        return ResultVOUtil.success(questionList);
    }

    /**
     * 更改问题的知识类目
     * @param questionId 问题id
     * @param knowledgeType 类目类型
     * @return
     */
    @ApiOperation(value = "更改问题的知识类目", notes = "更改问题的知识类目")
    @PostMapping("/changeType")
    public ResultVO changeKnowledgeType(@ApiParam(value = "问题id",required = true)
                                        @RequestParam("questionId") Integer questionId,
                                        @ApiParam(value = "类目类型",required = true)
                                        @RequestParam("knowledgeType") Integer knowledgeType) {
        questionService.changeKnowledgeType(questionId, knowledgeType);
        return ResultVOUtil.success();
    }

    /**
     * 给问题增加答案
     * @param questionId 问题id
     * @param answerId 答案id
     * @return
     */
    @ApiOperation(value = "给问题增加答案", notes = "更改问题的知识类目")
    @PostMapping("/setAnswerId")
    public ResultVO setAnswer(@ApiParam(value = "问题id",required = true)
                                        @RequestParam("questionId") Integer questionId,
                                        @ApiParam(value = "答案id",required = true)
                                        @RequestParam("answerId") Integer answerId) {
        questionService.setQuesAnswerId(questionId, answerId);
        return ResultVOUtil.success();
    }

    /**
     * 查询相应idDeleted状态的问题集
     * @param idDeleted 信息是否删除（0表示未删除，1表示删除）
     * @return 返回相应信息状态的问答集合
     */
    @ApiOperation(value = "查询相应idDeleted状态的问题集", notes = "查询相应idDeleted状态的问题集")
    @GetMapping("/idDeleted")
    public ResultVO findByIsDeleted(@ApiParam(value = "0--未删除,1--已删除,2~*--待设计",required = true)
                                        @RequestParam("idDeleted") Integer idDeleted) {
        List<Question> questionList = questionService.findQuesByIsDeleted(idDeleted);
        return ResultVOUtil.success(questionList);
    }

    /**
     * 设置问题状态 -> 已发布
     * @param id 问题id
     * @return null
     */
    @ApiOperation(value = "设置问题状态 -> 已发布", notes = "设置问题状态 -> 已发布")
    @PostMapping("/public")
    public ResultVO publicById(@ApiParam(value = "问题id",required = true)
                                @RequestParam("id") Integer id) {
        questionService.publicQuesById(id);
        return ResultVOUtil.success();
    }

    /**
     * 设置问题状态 -> 失效
     * @param id 问题id
     * @return null
     */
    @ApiOperation(value = "设置问题状态 -> 失效", notes = "设置问题状态 -> 失效")
    @PostMapping("/invalid")
    public ResultVO invalidById(@ApiParam(value = "问题id",required = true)
                               @RequestParam("id") Integer id) {
        questionService.invalidQuesById(id);
        return ResultVOUtil.success();
    }

    /**
     * @return 返回所有关键词
     */
    @ApiOperation(value = "获取所有关键词", notes = "获取所有关键词")
    @GetMapping("/keywords")
    public ResultVO getKeywords() {
        return ResultVOUtil.success(questionService.findKeywords());
    }

    /**
     * 删除问题
     * @param id 问题id
     * @return null
     */
    @ApiOperation(value = "删除问题", notes = "删除问题")
    @PostMapping("/delete")
    public ResultVO deleteById(@ApiParam(value = "问题id",required = true)
                                   @RequestParam("id") Integer id) {
        questionService.deleteQuesById(id);
        return ResultVOUtil.success();
    }
}
